<?php
// 初始化数据库连接
try {
    // 加载并验证数据库配置
    $dbConfig = require __DIR__.'/../config/database.php';
    if (!isset($dbConfig['connections']['mysql']['driver'])) {
        throw new Exception('数据库配置缺少driver参数');
    }

    $capsule = new Illuminate\Database\Capsule\Manager;
    $capsule->addConnection($dbConfig['connections']['mysql']);
    $capsule->setAsGlobal();
    $capsule->bootEloquent();

    // 测试数据库连接
    $connection = $capsule->getConnection();
    $connection->getPdo();
    echo "[".date('Y-m-d H:i:s')."] 数据库连接成功\n";
    echo "[".date('Y-m-d H:i:s')."] 使用驱动: ".$connection->getConfig('driver')."\n";
    return $capsule;
} catch (\Exception $e) {
    echo "[".date('Y-m-d H:i:s')."] 数据库连接失败: ".$e->getMessage()."\n";
    if (isset($dbConfig)) {
        echo "[".date('Y-m-d H:i:s')."] 当前配置: ".json_encode($dbConfig, JSON_PRETTY_PRINT)."\n";
    }
    exit(1);
}
